//
//  TabbarViewController.m
//  easystudy
//
//  Created by kane on 15/6/1.
//  Copyright (c) 2015年 kane. All rights reserved.
//

#import "TabbarViewController.h"
#import "HomeViewController.h"
#import "PersonCenterController.h"
#import "LaunchViewController.h"
#import "DiscoverViewController.h"
#import "SwipeBack.h"
@interface TabbarViewController ()
{
    NSArray *_titleArray;
    UIButton *tempButton;
    UIButton *homeBtn;
    UIButton *discoverBtn;
    UIButton *myBtn;
}

@property (nonatomic,strong) NSMutableArray *buttonArrays;

@end

@implementation TabbarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:kChangeNightMode object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccessComplete) name:NotificationLoginSuccess object:@1];
    
    _titleArray = [NSArray arrayWithObjects:@"首页", @"发现", @"我",nil];
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    UINavigationController *navController1 = [[UINavigationController alloc]
                                                  initWithRootViewController:homeVC];
    navController1.navigationBarHidden = YES;
    UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:[_titleArray objectAtIndex:0]
                                                              image:nil
                                                                tag:0];
    //ios7的方法
    homeVC.tabBarItem = tabBarItem1;
    
    
    DiscoverViewController *disVc = [[DiscoverViewController alloc] init];
    disVc.title = [_titleArray objectAtIndex:1];
    UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:disVc];
    navController2.navigationBarHidden = YES;
    disVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:[_titleArray objectAtIndex:1] image:nil tag:0];
    
    navController2.navigationBar.backgroundColor = [UIColor whiteColor];
    
    
    PersonCenterController *pcVC = [[PersonCenterController alloc] init];
    UINavigationController *navController4 = [[UINavigationController alloc]
                                                  initWithRootViewController:pcVC];
    navController4.navigationBarHidden = YES;
    UITabBarItem *tabBarItem4 = [[UITabBarItem alloc] initWithTitle:[_titleArray objectAtIndex:2]
                                                              image:nil
                                                                tag:0];
    pcVC.tabBarItem = tabBarItem4;
    
    
    NSArray *controllersArray = [NSArray arrayWithObjects:navController1,navController2,
                                 navController4,nil];
    
    self.viewControllers = controllersArray;
    
    [self customTabBar];
    
    [self setSelectedIndex:0];
    
//    LaunchViewController *launchVC = [[LaunchViewController alloc] init];
//    [self.view addSubview:launchVC.view];

}

-(void)loginSuccessComplete
{
    [self setSelectedIndex:0];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)hideRealTabBar{
    
    for(UIView *tempView in self.tabBar.subviews){
        
        if ([tempView isKindOfClass:[UITabBar class]]) {
            
            [tempView removeFromSuperview];
            
            break;
        }
    }
}

- (void)customTabBar{
    
    [self hideRealTabBar];
    UIView *tabBackgroudView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
    tabBackgroudView.backgroundColor = [UIColor lightGrayColor];
    [self.tabBar addSubview:tabBackgroudView];
    
    //创建按钮
    NSInteger viewCount = _titleArray.count > 5 ? 5 : _titleArray.count;
    self.buttonArrays = [NSMutableArray arrayWithCapacity:viewCount];
    
    CGFloat fXDistance = 0.0;
    double _width = (self.tabBar.frame.size.width - fXDistance * (viewCount - 1)) / viewCount;
    
    double _height = self.tabBar.frame.size.height;
    homeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    homeBtn.frame = CGRectMake(0, 0, _width, _height);
    [homeBtn setBackgroundColor:BackColor];
    homeBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [homeBtn setTitle:[_titleArray objectAtIndex:0]
                forState:UIControlStateNormal];
    [homeBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [homeBtn setTitleColor:[UIColor colorWithHex:0X46C09A] forState:UIControlStateSelected];
    [homeBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"main_tab_icon1_b"]] forState:UIControlStateNormal];
    [homeBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"main_tab_icon1_a"]] forState:UIControlStateSelected];
    [homeBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"main_tab_icon1_a"]] forState:UIControlStateReserved];
    [homeBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 40, 20, 35)];
    [homeBtn setTitleEdgeInsets:UIEdgeInsetsMake(32, -25, 0, 0)];
    [homeBtn addTarget:self
                   action:@selector(selectedTab:)
         forControlEvents:UIControlEventTouchUpInside];
    [self.tabBar addSubview:homeBtn];
    
    [self.buttonArrays addObject:homeBtn];
    
    discoverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    discoverBtn.frame = CGRectMake((_width + fXDistance), 0, _width, _height);
    [discoverBtn setBackgroundColor:BackColor];
    discoverBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [discoverBtn setTitle:[_titleArray objectAtIndex:1]
             forState:UIControlStateNormal];
    [discoverBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [discoverBtn setTitleColor:[UIColor colorWithHex:0X46C09A] forState:UIControlStateSelected];
    [discoverBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"main_tab_icon2_b"]] forState:UIControlStateNormal];
    [discoverBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"main_tab_icon2_a"]] forState:UIControlStateSelected];
    [discoverBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"main_tab_icon2_a"]] forState:UIControlStateReserved];
    [discoverBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 40, 20, 35)];
    [discoverBtn setTitleEdgeInsets:UIEdgeInsetsMake(32, -25, 0, 0)];
    [discoverBtn addTarget:self
                action:@selector(selectedTab:)
      forControlEvents:UIControlEventTouchUpInside];
    [self.tabBar addSubview:discoverBtn];
    
    [self.buttonArrays addObject:discoverBtn];
    
    myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    myBtn.frame = CGRectMake(2 *(_width + fXDistance), 0, _width, _height);
    [myBtn setBackgroundColor:BackColor];
    myBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [myBtn setTitle:[_titleArray objectAtIndex:2]
             forState:UIControlStateNormal];
    [myBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [myBtn setTitleColor:[UIColor colorWithHex:0X46C09A] forState:UIControlStateSelected];
    [myBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"main_tab_icon3_b"]] forState:UIControlStateNormal];
    [myBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"main_tab_icon3_a"]] forState:UIControlStateSelected];
    [myBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"main_tab_icon3_a"]] forState:UIControlStateReserved];
    [myBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 40, 20, 35)];
    [myBtn setTitleEdgeInsets:UIEdgeInsetsMake(32, -25, 0, 0)];
    [myBtn addTarget:self
                action:@selector(selectedTab:)
      forControlEvents:UIControlEventTouchUpInside];
    [self.tabBar addSubview:myBtn];
    [homeBtn setBackgroundColor:KKColor_DarkGray_White];
    [discoverBtn setBackgroundColor:KKColor_DarkGray_White];
    [myBtn setBackgroundColor:KKColor_DarkGray_White];
    [self.buttonArrays addObject:myBtn];
}

- (void)selectedTab:(UIButton *)sender{
    for (int i = 0; i < [self.buttonArrays count]; i++) {
        UIButton *tempButton1 = [self.buttonArrays objectAtIndex:i];
        if (tempButton1 == sender) {
            tempButton1.selected = YES;
            [super setSelectedIndex:i];
        }else{
            tempButton1.selected = NO;
        }
    }
}

-(void)setSelectedIndex:(NSUInteger)selectedIndex{
    
    if ([self.buttonArrays count] > 0) {
        UIButton *tempButton2 = [self.buttonArrays objectAtIndex:selectedIndex];
        [self selectedTab:tempButton2];
    }
}

#pragma mark - User Actions

- (void) changeColor{
    [homeBtn setBackgroundColor:KKColor_DarkGray_White];
    [discoverBtn setBackgroundColor:KKColor_DarkGray_White];
    [myBtn setBackgroundColor:KKColor_DarkGray_White];
}

@end
